package pl.koziolekweb.szkola.QZBuffImagWrite;

// QZ-BuffImagWrite/Images.java
 
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;

public class Images {
    public static void main(String[] args) {
        int w = 600; // <-- should be divisible by 4
        int h = 400; // <-- should be divisible by 4
        int first  = 0xFF660066, // purple
            second = 0xFF000000, // black
            circle = 0xFFFFFF00, // yellow
            letter = 0xFF660000; // dark red
        BufferedImage im =
                new BufferedImage(w,h,
                        BufferedImage.TYPE_INT_ARGB);

        // Just to show that we can set
        // any pixel or region of pixels
        int[] square = new int[(w/2)*(h/2)];
        for (int i = 0; i < (w/2)*(h/2); ++i)
            square[i] = first;
        im.setRGB(0,0,w/2,h/2,square,0,w/2);
        im.setRGB(w/2,h/2,w/2,h/2,square,0,w/2);
        for (int i = 0; i < (w/2)*(h/2); ++i)
            square[i] = second;
        im.setRGB(w/2,0,w/2,h/2,square,0,w/2);
        im.setRGB(0,h/2,w/2,h/2,square,0,w/2);

        // we can also draw figures and write strings
        Graphics2D g2 = (Graphics2D)im.getGraphics();
        setAntialiasing(g2);
        g2.setColor(new Color(circle));
        g2.fill(new Ellipse2D.Double(w/4,h/4,w/2,h/2));

        g2.setColor(new Color(letter));
        Font font = new Font("Monospaced",Font.BOLD,
                              Math.min(w/4,h/4));
        g2.setFont(font);
        FontRenderContext frc = g2.getFontRenderContext();
        String s = "\u0391\u03a9";
        TextLayout layout = new TextLayout(s,font,frc);
        Rectangle2D r = layout.getBounds();
        layout.draw(
            g2,(float)(w/2.0+r.getX()-r.getWidth()/2.0),
               (float)(h/2.0-r.getHeight()/2.0-r.getY()));

        // ...and save to a file
        String[] graphFormats = {"png","gif","jpg"};
        try {
            for (String f : graphFormats)
                ImageIO.write(im,f,new File("fig."+f));
        } catch(IOException err) {
            System.err.printf("Problems...");
            System.exit(1);
        }


        JFrame frame = new JFrame("Graphics files");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new MyPanel(im));
        frame.pack();
        frame.setVisible(true);
    }

    public static void setAntialiasing(Graphics g) {
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(
            RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setRenderingHint(
            RenderingHints.KEY_TEXT_ANTIALIASING,
            RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    }
}